/******************************************************************************
 * Copyright 2017 The Apollo Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

#include "modules/canbus/vehicle/nylg/protocol/mcu_drive_motor_feedback_msg_60.h"

#include "glog/logging.h"

#include "modules/drivers/canbus/common/byte.h"
#include "modules/drivers/canbus/common/canbus_consts.h"

namespace apollo {
namespace canbus {
namespace nylg {

using ::apollo::drivers::canbus::Byte;

Mcudrivemotorfeedbackmsg60::Mcudrivemotorfeedbackmsg60() {}
const int32_t Mcudrivemotorfeedbackmsg60::ID = 0x60;

void Mcudrivemotorfeedbackmsg60::Parse(const std::uint8_t* bytes, int32_t length,
                         ChassisDetail* chassis) const {
  chassis->mutable_nylg()->mutable_mcu_drive_motor_feedback_msg_60()->set_clamping_brake_status(clamping_brake_status(bytes, length));
  chassis->mutable_nylg()->mutable_mcu_drive_motor_feedback_msg_60()->set_mcu_reserved_1(mcu_reserved_1(bytes, length));
  chassis->mutable_nylg()->mutable_mcu_drive_motor_feedback_msg_60()->set_mcu_motor_error_grade(mcu_motor_error_grade(bytes, length));
  chassis->mutable_nylg()->mutable_mcu_drive_motor_feedback_msg_60()->set_mcu_energy_recovery_state(mcu_energy_recovery_state(bytes, length));
  chassis->mutable_nylg()->mutable_mcu_drive_motor_feedback_msg_60()->set_mcu_controltemp(mcu_controltemp(bytes, length));
  chassis->mutable_nylg()->mutable_mcu_drive_motor_feedback_msg_60()->set_mcu_idc(mcu_idc(bytes, length));
  chassis->mutable_nylg()->mutable_mcu_drive_motor_feedback_msg_60()->set_mcu_udc(mcu_udc(bytes, length));
}

// config detail: {'name': 'clamping_brake_status', 'offset': 0.0, 'precision': 1.0, 'len': 1, 'is_signed_var': False, 'physical_range': '[0|1]', 'bit': 43, 'type': 'bool', 'order': 'intel', 'physical_unit': 'N/A'}
bool Mcudrivemotorfeedbackmsg60::clamping_brake_status(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 5);
  int32_t x = t0.get_byte(3, 1);

  bool ret = x;
  return ret;
}

// config detail: {'name': 'mcu_reserved_1', 'offset': 0.0, 'precision': 1.0, 'len': 20, 'is_signed_var': False, 'physical_range': '[0|1048575]', 'bit': 44, 'type': 'int', 'order': 'intel', 'physical_unit': 'N/A'}
int Mcudrivemotorfeedbackmsg60::mcu_reserved_1(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 7);
  int32_t x = t0.get_byte(0, 8);

  Byte t1(bytes + 6);
  int32_t t = t1.get_byte(0, 8);
  x <<= 8;
  x |= t;

  Byte t2(bytes + 5);
  t = t2.get_byte(4, 4);
  x <<= 4;
  x |= t;

  int ret = x;
  return ret;
}

// config detail: {'name': 'mcu_motor_error_grade', 'offset': 0.0, 'precision': 1.0, 'len': 2, 'is_signed_var': False, 'physical_range': '[0|3]', 'bit': 41, 'type': 'int', 'order': 'intel', 'physical_unit': 'N/A'}
int Mcudrivemotorfeedbackmsg60::mcu_motor_error_grade(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 5);
  int32_t x = t0.get_byte(1, 2);

  int ret = x;
  return ret;
}

// config detail: {'name': 'mcu_energy_recovery_state', 'offset': 0.0, 'precision': 1.0, 'len': 1, 'is_signed_var': False, 'physical_range': '[0|1]', 'bit': 40, 'type': 'bool', 'order': 'intel', 'physical_unit': 'N/A'}
bool Mcudrivemotorfeedbackmsg60::mcu_energy_recovery_state(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 5);
  int32_t x = t0.get_byte(0, 1);

  bool ret = x;
  return ret;
}

// config detail: {'name': 'mcu_controltemp', 'offset': -50.0, 'precision': 1.0, 'len': 8, 'is_signed_var': False, 'physical_range': '[-50|205]', 'bit': 32, 'type': 'int', 'order': 'intel', 'physical_unit': '\xb6\xc8'}
int Mcudrivemotorfeedbackmsg60::mcu_controltemp(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 4);
  int32_t x = t0.get_byte(0, 8);

  int ret = x + -50.000000;
  return ret;
}

// config detail: {'name': 'mcu_idc', 'offset': -1000.0, 'precision': 0.1, 'len': 16, 'is_signed_var': False, 'physical_range': '[-1000|5553.5]', 'bit': 16, 'type': 'double', 'order': 'intel', 'physical_unit': 'A'}
double Mcudrivemotorfeedbackmsg60::mcu_idc(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 3);
  int32_t x = t0.get_byte(0, 8);

  Byte t1(bytes + 2);
  int32_t t = t1.get_byte(0, 8);
  x <<= 8;
  x |= t;

  double ret = x * 0.100000 + -1000.000000;
  return ret;
}

// config detail: {'name': 'mcu_udc', 'offset': 0.0, 'precision': 0.1, 'len': 16, 'is_signed_var': False, 'physical_range': '[0|6553.5]', 'bit': 0, 'type': 'double', 'order': 'intel', 'physical_unit': 'V'}
double Mcudrivemotorfeedbackmsg60::mcu_udc(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 1);
  int32_t x = t0.get_byte(0, 8);

  Byte t1(bytes + 0);
  int32_t t = t1.get_byte(0, 8);
  x <<= 8;
  x |= t;

  double ret = x * 0.100000;
  return ret;
}
}  // namespace nylg
}  // namespace canbus
}  // namespace apollo
